package com.gxpt.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gxpt.app.R;
import com.gxpt.app.bean.getregion_bean;

import java.util.List;

/**
 * Created by dufeng on 2017/4/16.
 */

public class city_sub_Adapter extends RecyclerView.Adapter {

    private List<getregion_bean.getregion> list;

    /**
     * Item的回调接口
     *
     */
    public interface MyItemClickListener {
        public void onItemClick(View view, int postion);
    }

    public interface MyItemLongClickListener {
        public void onItemLongClick(View view, int postion);
    }

    private MyItemClickListener mItemClickListener ; // 点击Item的回调对象

    private MyItemLongClickListener mItemLongClickListener ; // 点击Item的回调对象
    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(MyItemClickListener listener) {
        this.mItemClickListener = listener;
    }
    public void setOnItemLongClickListener(MyItemLongClickListener listener){
        this.mItemLongClickListener = listener;
    }


    Context context;
    public city_sub_Adapter(Context context,List<getregion_bean.getregion> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
//        View view = LayoutInflater.from(
//                viewGroup.getContext()).inflate(R.layout.item_list_type_main, null);

        View view = LayoutInflater.from(
                viewGroup.getContext()).inflate(R.layout.item_area_list, viewGroup, false);

        myViewHolder holder = new myViewHolder(view,mItemClickListener,mItemLongClickListener);
        holder.list_title = (TextView) view.findViewById(R.id.list_title);
        holder.type_id   = (TextView) view.findViewById(R.id.list_id);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        myViewHolder holder = (myViewHolder) viewHolder;


        holder.list_title.setText(list.get(position).name);
        holder.type_id  .setText(""+list.get(position).id);
        if(position == 0 ){
            holder.list_title.setBackgroundColor(context.getResources().getColor(R.color.white));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }



    public static class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,View.OnLongClickListener {

        public TextView list_title;
        public TextView type_id;
        private MyItemClickListener mListener;
        private MyItemLongClickListener mLongClickListener;


        public myViewHolder(View v,MyItemClickListener listener,MyItemLongClickListener longClickListener) {
            super(v);
            this.mListener = listener;
            this.mLongClickListener = longClickListener;
            v.setOnClickListener(this);
            v.setOnLongClickListener(this);
        }

        @Override
        public boolean onLongClick(View v) {
            if(mLongClickListener != null){
                mLongClickListener.onItemLongClick(v, getPosition());
            }
            return true;
        }

        @Override
        public void onClick(View v) {
            if(mListener != null){
                mListener.onItemClick(v,getPosition());
            }
        }
    }

    //refresh data
    public void reSetData(List<getregion_bean.getregion> datas){
        list.clear();
        list.addAll(datas);
        notifyItemInserted(list.size());
    }

    //添加数据
    public void addItem(List<getregion_bean.getregion> newDatas) {
        //mTitles.add(position, data);
        //notifyItemInserted(position);
        list.clear();
        notifyDataSetChanged();
        list.addAll(newDatas);
        notifyDataSetChanged();

    }

}
